<?php

class Zf24_Application_Resource_ModuleConfig extends Zend_Application_Resource_ResourceAbstract
{
    
    protected $_application;
    protected $_bootstrap;
    
    /**
     * Initialize
     *
     * @return Zend_Config
     */
    public function init()
    {
        $this->_bootstrap = $this->getBootstrap();
        $application = $this->_bootstrap->getApplication();
        
        define('MODULE_PATH',
            APPLICATION_PATH . DIRECTORY_SEPARATOR .
            'modules' . DIRECTORY_SEPARATOR .
            $this->_bootstrap->getModuleName()
            );
        
        return $this->_getModuleConfig();
    }
    
    /**
     * Load the module's config
     *
     * @return Zend_Config
     */
    protected function _getModuleConfig()
    {
        $modOptions = $this->_bootstrap->getOptions();
        $configPath =
            MODULE_PATH . DIRECTORY_SEPARATOR .
            'configs' . DIRECTORY_SEPARATOR;
        $configDirectory = new DirectoryIterator($configPath);
        
        foreach ( $configDirectory as $file ) {
            if ( $file->isFile() ) {
                
                $fileName = $file->getFilename();
                $options = $this->_loadOptions($configPath . $fileName);
                
                if ( ($len = strpos($fileName, '.')) !== false ) {
                    $configType = substr($fileName, 0, $len);
                } else {
                    $configType = $fileName;
                }
                
                if ( strtolower($configType) == 'module' ) {
                    $modOptions = array_merge($modOptions, $options);
                } else {
                    $modOptions['resources'][$configType] = $options;
                }
            }
        }
        $this->getBootstrap()->setOptions($modOptions);
        
        // FIXME module options should supercede application options (db etc)
    }

    /**
     * Load the config file
     *
     * @param string $fullpath
     * @return array
     */
    protected function _loadOptions($fullpath)
    {
        if ( file_exists($fullpath) ) {
            switch ( substr(trim(strtolower($fullpath)), -3) ) {
                case 'ini':
                    $cfg = new Zend_Config_Ini(
                        $fullpath,
                        $this->getBootstrap()->getEnvironment()
                        );
                    break;
                case 'xml':
                    $cfg = new Zend_Config_Xml(
                        $fullpath,
                        $this->getBootstrap()->getEnvironment()
                        );
                    break;
                default:
                    throw new Zend_Config_Exception('Invalid format for config file');
                    break;
            }
        } else {
            throw new Zend_Application_Resource_Exception('File does not exist');
        }
        return $cfg->toArray();
    }
    
}
